<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="任务名称" prop="taskName">
        <el-input
          v-model="queryParams.taskName"
          placeholder="请输入任务名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属服务" prop="serviceId">
        <el-select v-model="queryParams.serviceId" placeholder="请选择服务" clearable>
          <el-option
            v-for="service in serviceList"
            :key="service.id"
            :label="service.serviceName"
            :value="service.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="任务状态" prop="taskStatus">
        <el-select v-model="queryParams.taskStatus" placeholder="请选择任务状态" clearable>
          <el-option
            v-for="dict in dict.type.task_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['backend:t_task:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['backend:t_task:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['backend:t_task:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['backend:t_task:export']"
        >导出</el-button>
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="t_taskList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="任务ID" align="center" prop="id" />-->
<!--      <el-table-column label="任务名称" align="center" prop="taskName" />-->
      <el-table-column label="任务名称" align="center" prop="taskName">
        <template #default="scope">
          <el-button type="text" @click="handleShopBidding(scope.row)">{{ scope.row.taskName }}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="任务描述" align="center" prop="taskInfo" />
      <el-table-column label="发布人ID" align="center" prop="publishId" />
      <el-table-column label="发布人姓名" align="center" prop="publishName" />
      <el-table-column label="发布人电话" align="center" prop="publishPhone" />
<!--      <el-table-column label="所属服务ID" align="center" prop="serviceId" />-->
      <el-table-column label="所属服务名称" align="center" prop="serviceName" />
      <el-table-column label="任务价格" align="center" prop="taskPrice" />
      <el-table-column label="任务类型" align="center" prop="taskType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.task_type" :value="scope.row.taskType"/>
        </template>
      </el-table-column>
      <el-table-column label="任务开始日期" align="center" prop="taskDateStart" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.taskDateStart, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="任务结束日期" align="center" prop="taskDateEnd" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.taskDateEnd, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="任务地址" align="center" prop="taskAddress" />
      <el-table-column label="任务生效截止日期" align="center" prop="taskEffectDeadline" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.taskEffectDeadline, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="任务状态" align="center" prop="taskStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.task_status" :value="scope.row.taskStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.taskStatus == 1"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleAudit(scope.row)"
            v-hasPermi="['backend:t_task:edit']"
          >审核</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改任务对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="任务名称" prop="taskName">
          <el-input v-model="form.taskName" placeholder="请输入任务名称" disabled />
        </el-form-item>
        <el-form-item label="任务描述" prop="taskInfo">
          <el-input v-model="form.taskInfo" placeholder="请输入任务描述" disabled />
        </el-form-item>
        <el-form-item label="发布人ID" prop="publishId">
          <el-input v-model="form.publishId" placeholder="请输入发布人ID" disabled />
        </el-form-item>
        <el-form-item label="发布人姓名" prop="publishName">
          <el-input v-model="form.publishName" placeholder="请输入发布人姓名" disabled />
        </el-form-item>
        <el-form-item label="发布人电话" prop="publishPhone">
          <el-input v-model="form.publishPhone" placeholder="请输入发布人电话" disabled />
        </el-form-item>
        <el-form-item label="所属服务ID" prop="serviceId">
          <el-input v-model="form.serviceId" placeholder="请输入所属服务ID" disabled />
        </el-form-item>
        <el-form-item label="所属服务名称" prop="serviceName">
          <el-input v-model="form.serviceName" placeholder="请输入所属服务名称" disabled />
        </el-form-item>
        <el-form-item label="任务价格" prop="taskPrice">
          <el-input v-model="form.taskPrice" placeholder="请输入任务价格" disabled />
        </el-form-item>
        <el-form-item label="任务开始日期" prop="taskDateStart">
          <el-date-picker clearable disabled
            v-model="form.taskDateStart"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择任务开始日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="任务结束日期" prop="taskDateEnd">
          <el-date-picker clearable disabled
            v-model="form.taskDateEnd"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择任务结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="任务地址" prop="taskAddress">
          <el-input v-model="form.taskAddress" placeholder="请输入任务地址" disabled />
        </el-form-item>
        <el-form-item label="任务生效截止日期" prop="taskEffectDeadline">
          <el-date-picker clearable disabled
            v-model="form.taskEffectDeadline"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择任务生效截止日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="任务状态" prop="taskStatus">
          <el-select v-model="form.taskStatus" placeholder="请选择任务状态" disabled>
            <el-option
              v-for="dict in dict.type.task_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormForAuditPass">通过</el-button>
        <el-button type="danger" @click="submitFormForAuditNoPass">不通过</el-button>
      </div>
    </el-dialog>

    <!-- 店铺竞标情况 -->
    <el-dialog :visible.sync="shopBiddingVisible" title="竞标详情" width="80%" top="5vh" custom-class="workflow-dialog">
      <shopBidding v-if="shopBiddingVisible" :taskId="taskId"></shopBidding>
    </el-dialog>

  </div>
</template>

<script>
import {listT_task, getT_task, delT_task, addT_task, updateT_task, auditTask} from "@/api/backend/t_task"
import {getServiceList} from "@/api/backend/t_service";
import shopBidding from "@/views/backend/t_shop_bidding/index.vue";

export default {
  name: "T_task",
  components: {shopBidding},
  dicts: ['task_status', 'task_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 任务表格数据
      t_taskList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 服务列表
      serviceList: [],
      //店铺竞标情况相关参数
      shopBiddingVisible: false,
      taskId: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        taskName: null,
        taskInfo: null,
        publishId: null,
        publishName: null,
        publishPhone: null,
        serviceId: null,
        serviceName: null,
        taskPrice: null,
        taskType: null,
        taskDateStart: null,
        taskDateEnd: null,
        taskAddress: null,
        taskEffectDeadline: null,
        taskStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    }
  },
  created() {
    this.getList();
    this.getServiceList();
  },
  methods: {
    /** 查询任务列表 */
    getList() {
      this.loading = true
      listT_task(this.queryParams).then(response => {
        this.t_taskList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        taskName: null,
        taskInfo: null,
        publishId: null,
        publishName: null,
        publishPhone: null,
        serviceId: null,
        serviceName: null,
        taskPrice: null,
        taskType: null,
        taskDateStart: null,
        taskDateEnd: null,
        taskAddress: null,
        taskEffectDeadline: null,
        taskStatus: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 获取服务列表 */
    getServiceList() {
      getServiceList().then(response => {
        this.serviceList = response.data;
      })
    },
    /** 审核按钮操作 */
    handleAudit(row) {
      this.reset()
      const id = row.id || this.ids;
      getT_task(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "任务审核";
      })
    },
    /** 点击查询商店竞标情况 */
    handleShopBidding(row) {
      this.taskId = row.id;
      this.shopBiddingVisible = true;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加任务"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getT_task(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改任务"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateT_task(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addT_task(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 审核通过按钮 */
    submitFormForAuditPass() {
      this.form.taskStatus = 2;
      auditTask(this.form).then(response => {
        this.$modal.msgSuccess("已审核")
        this.open = false
        this.getList()
      })
    },
    /** 审核不通过按钮 */
    submitFormForAuditNoPass() {
      this.form.taskStatus = 20;
      auditTask(this.form).then(response => {
        this.$modal.msgSuccess("已审核")
        this.open = false
        this.getList()
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除任务编号为"' + ids + '"的数据项？').then(function() {
        return delT_task(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('backend/t_task/export', {
        ...this.queryParams
      }, `t_task_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
